package com.zax.model;

import com.zax.utils.PriceUtil;

import java.util.*;

public class Order {
    private int id;
    private float total;//总价
    private int amount;//商品总数
    private int status;//(1未付款/2已付款/3已发货/4已完成)
    private int paytype;//(1微信/2支付宝/3货到付款)
    private String name;//收货人
    private String phone;
    private String address;
    private Date datetime;//下单时间
    private User user;//下单用户
    private Map<Integer,Orderitem> itemMap = new HashMap<Integer,Orderitem>();
    private List<Orderitem> itemList = new ArrayList<Orderitem>();

    public Map<Integer, Orderitem> getItemMap() {
        return itemMap;
    }

    public void setUsername(String username){
        user = new User();
        user.setUsername(username);
    }

    public void setItemMap(Map<Integer, Orderitem> itemMap) {
        this.itemMap = itemMap;
    }

    public List<Orderitem> getItemList() {
        return itemList;
    }

    public void setItemList(List<Orderitem> itemList) {
        this.itemList = itemList;
    }

    public void addGoods(Goods g){
        if (itemMap.containsKey(g.getId())){
            Orderitem item = itemMap.get(g.getId());
            item.setAmount(item.getAmount()+1);
        }else {
            Orderitem item = new Orderitem(g.getPrice(),1,g,this);
            itemMap.put(g.getId(),item);
        }
        amount++;
//        total += g.getPrice();
        total = PriceUtil.add(total,g.getPrice());
    }

    public void lessenGoods(int goodsid){
        if (itemMap.containsKey(goodsid)){
            Orderitem item = itemMap.get(goodsid);
            item.setAmount(item.getAmount()-1);
            amount--;
//            total -= item.getPrice();
            total = PriceUtil.subtract(total,item.getPrice());
            if (item.getAmount() <=0){
                itemMap.remove(goodsid);
            }
        }
    }
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public float getTotal() {
        return total;
    }

    public void setTotal(float total) {
        this.total = total;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getPaytype() {
        return paytype;
    }

    public void setPaytype(int paytype) {
        this.paytype = paytype;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Date getDatetime() {
        return datetime;
    }

    public void setDatetime(Date datetime) {
        this.datetime = datetime;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public Order() {
    }


}
